package bdz.projects.cbt.data
{
	import bdz.common.events.GenericEvent;
	import bdz.common.events.XMLLoaderEvent;
	import bdz.common.model.AppEnvironment;
	import bdz.common.model.XMLLoader;
	import bdz.projects.cbt.core.CBTPlayer;
	
	import flash.events.EventDispatcher;
	import flash.net.URLVariables;

	public class CBTData extends EventDispatcher{
		
		private static var instance:CBTData;
		
		public function CBTData(se:SingletonEnforcer){
			super();
		}
		public static function getInstance():CBTData{
			if(instance == null){
				instance = new CBTData(new SingletonEnforcer());
			}
			return instance;				
		}
		private function topScoreLoaded(evt:XMLLoaderEvent):void{
			
			
			dispatchEvent(new GenericEvent("topScoreLoaded", new XML(evt.data)));
		}
		private function singleTopScoreLoaded(evt:XMLLoaderEvent):void{
			dispatchEvent(new GenericEvent("singleTopScoreLoaded", new XML(evt.data)));
		}
		private function onUserInserted(evt:XMLLoaderEvent):void{
			
			dispatchEvent(new GenericEvent("userInserted", new XML(evt.data)));
		}
		private function onEmailSent(evt:XMLLoaderEvent):void{
			
		}
		public function sendToFriends(arr:Array, p:CBTPlayer):void{
			var xloader:XMLLoader = new XMLLoader("onEmailSent");
			xloader.addEventListener("onEmailSent", onEmailSent);
			xloader.contentType = "application/octet-stream";
			xloader.location = AppEnvironment.getEnv().getSetting("baseURL")+"api.php";
			var tosend:XML = <df_user/>;
			tosend.@method = "send.to.friends";
			tosend.@sender = p.playerName;
			var xmllist:XMLList = new XMLList();
			for (var i:int = 0;i<arr.length;i++){
				xmllist[i] = new XML("<friend/>");
				xmllist[i].name = arr[i].name;
				xmllist[i].email = arr[i].email;
			}
			tosend.xlist = new XMLList(xmllist);
			xloader.sendCompressed(tosend.toString());

		}
		public function insertNewUser(p:CBTPlayer):void{
			
			var xm:XMLLoader = new XMLLoader("userInserted");
			xm.addEventListener("userInserted", onUserInserted);
			//xm.contentType = "application/octet-stream";
			xm.location = AppEnvironment.getEnv().getSetting("baseURL")+"api.php";
			
			
			/*
			var tosend:XML = <cbt_player/>;
			tosend.@method = "add.topscores";
			tosend.playername = p.playerName;
			tosend.money = p.cash.value+p.paypal.value;
			tosend.feedback = p.feedback;
			tosend.mostsold = p.mostSold;
			tosend.email = p.email;
			tosend.fullname = p.realName;
			tosend.gametype = p.maxDays;
			*/
			var tosend:URLVariables = new URLVariables();
			tosend.method = "add.topscores";
			tosend.playername = p.playerName;
			tosend.money = p.cash.value+p.paypal.value;
			tosend.feedback = p.feedback;
			tosend.mostsold = p.mostSold;
			tosend.email = p.email;
			tosend.fullname = p.realName;
			tosend.gametype = p.maxDays;
			trace("sending score >>> "+p.maxDays);
			//xm.sendCompressed(tosend.toString());
			xm.send(tosend);
			
		}
		public function getSingleTopScore(gtype:Number = 30):void{
			
			var xm:XMLLoader = new XMLLoader("singleTopScoreLoaded");
			xm.addEventListener("singleTopScoreLoaded", singleTopScoreLoaded);
			xm.location = AppEnvironment.getEnv().getSetting("baseURL")+"api.php";
			
			var uvars:URLVariables = new URLVariables();
			uvars.method = "get.top.all";
			uvars.gametype = gtype;
			xm.send(uvars);
		}
		public function getTopScores():void{
			var xm:XMLLoader = new XMLLoader("topScoreLoaded");
			xm.addEventListener("topScoreLoaded", topScoreLoaded);
			//xm.contentType = "application/octet-stream";
			xm.location = AppEnvironment.getEnv().getSetting("baseURL")+"api.php";
			//var tosend:XML = <cbt_topscores/>
			//tosend.@method = "get.topscores";
			//tosend.filler = "Cross Border Trader";
			var uvars:URLVariables = new URLVariables();
			uvars.method = "get.topscores";
			
			xm.send(uvars);
			
			//xm.sendCompressed(tosend.toString());
		}
		
	}
}
internal class SingletonEnforcer {}